package com.siao.component
{
	import com.siao.display.Align;
	import com.siao.display.LayoutElement;
	
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	/**
	 * 对话框组件
	 * @author SiaoLeon
	 */
	public class Dialog extends LayoutElement
	{
		// -------------------------------------------------------------------------------------------------------- 静态常量
		
		// -------------------------------------------------------------------------------------------------------- 静态变量
		
		// -------------------------------------------------------------------------------------------------------- 静态方法
		
		// -------------------------------------------------------------------------------------------------------- 静态函数
		
		// -------------------------------------------------------------------------------------------------------- 属性
		
		/**
		 * 确认回调方法
		 * @default
		 */
		public var onConfirm:Function;
		
		/**
		 * 取消回调方法
		 * @default
		 */
		public var onCancel:Function;
		
		/**
		 * 倒计时间隔回调方法
		 * @default
		 */
		public var onInterval:Function;
		
		/**
		 * 倒计时超时回调方法
		 * @default
		 */
		public var onTimeout:Function;
		
		/**
		 * 添加到舞台回调方法
		 * @default
		 */
		public var onAdded:Function;
		
		/**
		 * 从舞台移除回调方法
		 * @default
		 */
		public var onRemoved:Function;
		
		/**
		 * 数据
		 * @default
		 */
		public var data:Object;
		
		// -------------------------------------------------------------------------------------------------------- 变量
		
		protected var timer:Timer;
		
		// -------------------------------------------------------------------------------------------------------- 构造
		
		/**
		 * 构造函数
		 * @param data 对话框数据
		 * @param delay 倒计时间隔时间（毫秒）
		 * @param repeat 倒计时间隔次数
		 */
		public function Dialog(data:Object=null, delay:Number=0, repeat:int=0)
		{
			super();
			
			this.data = data;
			this.timer = new Timer(delay, repeat);
			
			this.alignH = Align.CENTER;
			this.alignV = Align.CENTER;
		}
		
		// -------------------------------------------------------------------------------------------------------- 方法
		
		/**
		 * 设置回调方法
		 * @param confirm 确认回调方法
		 * @param cancel 取消回调方法
		 * @param interval 倒计时间隔回调方法
		 * @param timeout 倒计时超时回调方法
		 * @param added 添加到舞台回调方法
		 * @param removed 从舞台移除回调方法
		 */
		public function setCallback(confirm:Function=null, cancel:Function=null, interval:Function=null, timeout:Function=null, added:Function=null, removed:Function=null):Dialog
		{
			this.onConfirm = confirm;
			this.onCancel = cancel;
			this.onInterval = interval;
			this.onTimeout = timeout;
			this.onAdded = added;
			this.onRemoved = removed;
			
			return this;
		}
		
		/**
		 * 确认
		 */
		public function confirm(event:Event=null):void
		{
			this.remove();
			this.callback(this.onConfirm);
		}
		
		/**
		 * 取消
		 */
		public function cancel(event:Event=null):void
		{
			this.remove();
			this.callback(this.onCancel);
		}
		
		// -------------------------------------------------------------------------------------------------------- 函数
		
		override protected function onAddedToStage():void
		{
			this.timer.addEventListener(TimerEvent.TIMER, onTimer);
			this.timer.addEventListener(TimerEvent.TIMER_COMPLETE, onTimerComplete);
			
			if (this.timer.delay > 0)
			{
				this.timer.reset();
				this.timer.start();
			}
			
			this.callback(this.onAdded);
		}
		
		override protected function onRemovedFromStage():void
		{
			this.timer.removeEventListener(TimerEvent.TIMER, onTimer);
			this.timer.removeEventListener(TimerEvent.TIMER_COMPLETE, onTimerComplete);
			
			this.timer.stop();
			
			this.callback(this.onRemoved);
		}
		
		/**
		 * 计时器间隔时
		 * @param event
		 */
		protected function onTimer(event:TimerEvent):void
		{
			this.callback(this.onInterval);
		}
		
		/**
		 * 计时器完成时
		 * @param event
		 */
		protected function onTimerComplete(event:TimerEvent):void
		{
			this.callback(this.onTimeout);
			this.remove();
		}
		
		/**
		 * 回调方法
		 * @param fun
		 */
		private function callback(fun:Function):void
		{
			if (fun != null)
			{
				if (this.data == null)
				{
					fun.call();
				}
				else
				{
					fun.call(this, data);
				}
			}
		}
	
	}
}
